package api

import (
	"certificate/service"
	"certificate/vo"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"log"
)


// MeituanReady @Summary 美团准备
// @Description 美团验券准备
// @Tags meituan
// @Accept  json
// @Produce  json
// @Param receiptCode body vo.MeituanCertificatePrepare true "receiptCode"
// @Success 200 {string} string "{"code":20000,"data":{},"msg":"success"}"
// @Router /meituan/ready [post]
func MeituanReady(c *gin.Context) {
	businessId := viper.GetInt("meituan.token.businessId")
	appAuthToken := viper.GetString("meituan.token.appAuthToken")
	prepare := vo.MeituanCertificatePrepare{}

	if err := c.ShouldBindJSON(&prepare); err != nil {
		log.Printf("[ERROR] Binding JSON failed: %v", err)
		ResponseError(c, "Invalid request payload", 21000)
		return
	}

	client := service.NewMeituanClient(businessId, appAuthToken)

	bytes, err := client.Prepare(prepare.ReceiptCode)
	if err != nil {
		log.Printf("[ERROR] Prepare operation failed: %v", err)
		ResponseError(c, err.Error(), 21001)
		return
	}

	var data = make(map[string]interface{})
	if err := json.Unmarshal(bytes, &data); err != nil {
		log.Printf("[ERROR] Unmarshalling data failed: %v", err)
		ResponseError(c, "Failed to parse server response", 21002)
		return
	}

	ResponseSuccess(c, data)
}

// MeituanVerify @Summary 美团验券
// @Description 美团验券
// @Tags meituan
// @Accept  json
// @Produce  json
// @Param receiptCode body vo.MeituanCertificatePrepare true "receiptCode"
// @Success 200 {string} string "{"code":20000,"data":{},"msg":"success"}"
// @Router /meituan/verify [post]
func MeituanVerify(c *gin.Context) {
	businessId := viper.GetInt("meituan.token.businessId")
	appAuthToken := viper.GetString("meituan.token.appAuthToken")
	prepare := vo.MeituanCertificatePrepare{}

	if err := c.ShouldBindJSON(&prepare); err != nil {
		log.Printf("[ERROR] Binding JSON failed: %v", err)
		ResponseError(c, "Invalid request payload", 22001)
		return
	}

	client := service.NewMeituanClient(businessId, appAuthToken)

	verify, err := client.Verify(prepare.ReceiptCode)
	if err != nil {
		log.Printf("[ERROR] Verify operation failed: %v", err)
		ResponseError(c, err.Error(), 22002)
		return
	}

	var data = make(map[string]interface{})
	if err := json.Unmarshal(verify, &data); err != nil {
		log.Printf("[ERROR] Unmarshalling data failed: %v", err)
		ResponseError(c, "Failed to parse server response", 22003)
		return
	}

	ResponseSuccess(c, data)
}

// MeituanCancel @Summary 美团取消
// @Description 美团取消
// @Tags meituan
// @Accept  json
// @Produce  json
// @Param receiptCode body vo.MeituanCertificateCancel true "receiptCode and dealId"
// @Success 200 {string} string "{"code":20000,"data":{},"msg":"success"}"
// @Router /meituan/cancel [post]
func MeituanCancel(c *gin.Context) {
	businessId := viper.GetInt("meituan.token.businessId")
	appAuthToken := viper.GetString("meituan.token.appAuthToken")
	cancel := vo.MeituanCertificateCancel{}

	if err := c.ShouldBindJSON(&cancel); err != nil {
		log.Printf("[ERROR] Binding JSON failed: %v", err)
		ResponseError(c, "Invalid request payload", 20001)
		return
	}

	client := service.NewMeituanClient(businessId, appAuthToken)

	res, err := client.Cancel(cancel.ReceiptCode, cancel.DealId)
	if err != nil {
		log.Printf("[ERROR] Cancel operation failed: %v", err)
		ResponseError(c, err.Error(), 20002)
		return
	}

	var data = make(map[string]interface{})
	if err := json.Unmarshal(res, &data); err != nil {
		log.Printf("[ERROR] Unmarshalling data failed: %v", err)
		ResponseError(c, "Failed to parse server response", 20003)
		return
	}

	ResponseSuccess(c, data)
}

// MeituanGet @Summary 美团查询
// @Description 美团查询
// @Tags meituan
// @Accept  json
// @Produce  json
// @Param receiptCode body vo.MeituanCertificatePrepare true "receiptCode"
// @Success 200 {string} string "{"code":20000,"data":{},"msg":"success"}"
// @Router /meituan/get [post]
func MeituanGet(c *gin.Context) {
	businessId := viper.GetInt("meituan.token.businessId")
	appAuthToken := viper.GetString("meituan.token.appAuthToken")
	prepare := vo.MeituanCertificatePrepare{}

	if err := c.ShouldBindJSON(&prepare); err != nil {
		log.Printf("[ERROR] Binding JSON failed: %v", err)
		ResponseError(c, "Invalid request payload", 23000)
		return
	}

	client := service.NewMeituanClient(businessId, appAuthToken)

	get, err := client.Get(prepare.ReceiptCode)
	if err != nil {
		log.Printf("[ERROR] Get operation failed: %v", err)
		ResponseError(c, err.Error(), 23001)
		return
	}

	var data = make(map[string]interface{})
	if err := json.Unmarshal(get, &data); err != nil {
		log.Printf("[ERROR] Unmarshalling data failed: %v", err)
		ResponseError(c, "Failed to parse server response", 23002)
		return
	}

	ResponseSuccess(c, data)
}